﻿using BizControl.Library.BusinessObjects;
using Csla.Xaml;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism;
using System;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows.Input;
using BizControl.Library.BusinessObjects.Categories;

namespace BizControl.Presentation.CategoriesModule.ViewModels
{
  [Export]
  public class CategoriesViewModel : ViewModel<CategoryList>, INavigationAware, IRegionMemberLifetime, IPartImportsSatisfiedNotification
  {
    private readonly DelegateCommand<Guid?> _editCategoryCommand;
    private readonly DelegateCommand _refreshListCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public CategoriesViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      _editCategoryCommand = new DelegateCommand<Guid?>(EditCategory);
      _refreshListCommand = new DelegateCommand(RefreshList);
    }

    #region Commands

    public ICommand EditCategoryCommand
    {
      get { return this._editCategoryCommand; }
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
    }

    public ICommand RefreshListCommand
    {
      get { return this._refreshListCommand; }
    }

    private void EditCategory(Guid? activityId)
    {
      //if (!string.IsNullOrEmpty(activityId.ToString()))
      //{
      //  var uriQuery = new UriQuery();
      //  uriQuery.Add(NavigationParametersNames.IdName, activityId.ToString());
      //  var uri = new Uri(ViewNames.ActivityEditViewName + uriQuery.ToString(), UriKind.Relative);
      //  this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      //}
    }

    #endregion

    CategoryList GetList()
    {
      return CategoryList.GetCategoryList();
    }

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      RefreshList();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
